func! Pattern_escape()
" python << endpython
" import vim
" def either(c):
" 	return '%s%s'%('\\',c) if c in ('[', ']', '.', '*', '/', '\\', '~', '`') else c
" 
" def vim_pattern_escape(arg, n):
" 	ded=vim.eval("escape(g:replace_"+arg+"_str, '\')")
" 	narg=''.join(map(either,ded))
" 	vim.command(n+"match FindReplace_"+arg+" /"+narg+"/")
" 	print narg
" 
" def vim_pattern_match(pattern, group, mid):
" 	ded=vim.eval("escape("+pattern+", '\')")
" 	npattern=''.join(map(either, ded))
" 
" 	ngroup=vim.eval("string("+group+")")
" 	mid=vim.eval("string("+mid+")")
" 	group=vim.eval(group)
" 
" 	vim.command(":let g:match_"+group+"=matchadd("+ngroup+", '"+npattern+"', "+mid+")")
" 	#print ngroup+" "+npattern+" "+mid
" 
" endpython
endfunc

func! Find_And_Replace() range
python << endpython
import vim
def find_and_replace():
	buf = vim.current.buffer
	(lnum1, col1) = buf.mark('<')
	(lnum2, col2) = buf.mark('>')
	str_range=str(lnum1)+","+str(lnum2)
	ded=vim.eval("escape(g:replace_src_str, '\')")
	narg=''.join(map(either,ded))
	flag=vim.eval("input('method? [g|c|i|y]: ')")
	vim.command(str_range+"g/\%V"+narg+"/s/\%V"+narg+"/\=g:replace_dest_str/"+flag)
	vim.command("call matchdelete(1)")
	vim.command("call matchdelete(2)")
	#vim.command("hi clear FindReplace_src")
	#vim.command("hi clear FindReplace_dest")

endpython
endfunc

" func! Py_ThomasMatch(group, range) range
" python << endpython
" import vim
" def find_and_replace():
" 	buf = vim.current.buffer
" 	(lnum1, col1) = buf.mark('<')
" 	(lnum2, col2) = buf.mark('>')
" 	str_range=str(lnum1)+","+str(lnum2)
" 	ded=vim.eval("escape(g:replace_src_str, '\')")
" 	narg=''.join(map(either,ded))
" 	flag=vim.eval("input('method? [g|c|i|y]: ')")
" 	vim.command(str_range+"g/\%V"+narg+"/s/\%V"+narg+"/\=g:replace_dest_str/"+flag)
" 	vim.command("call matchdelete(1)")
" 	vim.command("call matchdelete(2)")
" 	#vim.command("hi clear FindReplace_src")
" 	#vim.command("hi clear FindReplace_dest")
" 
" endpython
" endfunc

"func! Find_And_Replace() range
	"exe "escape(g:replace_src_str, '\')"
	"narg=''.join(map(either,ded))
	"flag=vim.eval("input('method? [g|c|i|y]: ')")
	"vim.command("g/"+narg+"/s/"+narg+"/\=g:replace_dest_str/"+flag)
	"vim.command("call matchdelete(1)")
	"vim.command("call matchdelete(2)")
"endfunc

fun! GetRange()
python << EOF

import vim

buf = vim.current.buffer
(lnum1, col1) = buf.mark('<')
(lnum2, col2) = buf.mark('>')
lines = vim.eval('getline({}, {})'.format(lnum1, lnum2))
lines[0] = lines[0][col1:]
lines[-1] = lines[-1][:col2]
print "\n".join(lines)

EOF
endfun
